package test.yzx.pproject.act1;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;

import test.yzx.pproject.R;
import test.yzx.pproject.ViewAnimatorUtil;
import test.yzx.pproject.util.GenericCallback2;

public class Anim3Activity extends AppCompatActivity {

    private ViewGroup containerLayout;
    private final int childCount = 5;
    private boolean isOver = false;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_a3);
        containerLayout = (ViewGroup) findViewById(R.id.container);

        callbackChildren(new GenericCallback2<Integer, View>() {
            public void callback(Integer integer, View view) {
                view.setAlpha(0f);
            }
        });

        getWindow().getDecorView().postDelayed(new Runnable() {
            public void run() {
                startAnimIn();
            }
        },100);

    }


    private int animDuration = 600;
    private int delayOnAnimNextView = 150;
    private int delayOnAnimNextType = 350;
    private int inAnimCount = 0;


    private void startAnimIn(){
        if(isOver) return ;
        hookBeforeAnimInStart(inAnimCount++);
        callbackChildren(new GenericCallback2<Integer, View>() {
            public void callback(final Integer position, View view) {
                /// reset view bounds before anim ///
                view.setTranslationY(-view.getHeight());
                view.setAlpha(0f);
                /// start anim ///
                ViewAnimatorUtil.BoundsChangeParam params = new ViewAnimatorUtil.BoundsChangeParam();
                params.alpha = 1f;
                params.translateY = 0f;
                params.delay = delayOnAnimNextView*position;
                params.duration = animDuration;
                ViewAnimatorUtil.goBoundsTogether(view, params, new ViewAnimatorUtil.SimpleStatusChangedListener() {
                    public void onEnd() {
                        if(position == childCount - 1)
                            mHandler.postDelayed(new Runnable() {
                                public void run() {
                                    startAnimOut();
                                }
                            },delayOnAnimNextType);
                    }
                });
            }
        });
    }
    private void startAnimOut(){
        if(isOver) return ;
        callbackChildren(new GenericCallback2<Integer, View>() {
            public void callback(final Integer position, View view) {
                /// reset view bounds before anim ///
                view.setTranslationY(0);
                view.setAlpha(1f);
                /// start anim ///
                ViewAnimatorUtil.BoundsChangeParam params = new ViewAnimatorUtil.BoundsChangeParam();
                params.alpha = 0f;
                params.translateY = (float)view.getHeight();
                params.delay = delayOnAnimNextView*position;
                params.duration = animDuration;
                ViewAnimatorUtil.goBoundsTogether(view, params, new ViewAnimatorUtil.SimpleStatusChangedListener() {
                    public void onEnd() {
                        if(position == childCount-1)
                            mHandler.postDelayed(new Runnable() {
                                public void run() {
                                    startAnimIn();
                                }
                            },delayOnAnimNextType);
                    }
                });
            }
        });
    }

    private void callbackChildren(GenericCallback2<Integer, View> callback){
        for (int i = 0; i < childCount; i++)
            callback.callback(i, containerLayout.getChildAt(i));
    }

    private void hookBeforeAnimInStart(int animCount){
        callbackChildren(new GenericCallback2<Integer, View>() {
            public void callback(Integer integer, View view) {
               view.setBackgroundColor(Color.argb(255, ((int)(Math.random()*255)), ((int)(Math.random()*255)), ((int)(Math.random()*255))));
            }
        });
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        isOver = true;
        mHandler.removeCallbacksAndMessages(null);
    }

}
